package com.hdax.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CreatUser;
import com.hdax.entity.Role;
import com.hdax.entity.UpdateUser;
import com.hdax.entity.User;
import com.hdax.service.impl.RoleServiceImpl;
import com.hdax.service.impl.UserServiceImpl;
import com.hdax.until.RandomSalt;
import com.hdax.until.UserIdUntil;
import com.hdax.vo.ActiveUser;
import com.hdax.vo.PwdVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl service;
    @Autowired
    private RoleServiceImpl roleService;

    @RequestMapping("/findUser")
    public List<User> findUser() {
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("disable", "false");
        return service.list(queryWrapper);
    }
    @PostMapping("/findUserByCustomerId")
    public User findUserByCustomerId(Integer cusId){
        return service.findUserByCustomerId(cusId);
    }


    @GetMapping("/main")
    public ModelAndView add(){
        return new ModelAndView("user/index");
    }

    @RequestMapping("/list")
    public IPage<User> selectUser( User user,Integer page,Integer limit ){
        Page<User> p=new Page<>(page,limit);
        IPage<User> iPage = service.selectPageVo(p, user);
        for (User record : iPage.getRecords()) {
            if(record.getCreatUser()==null){
                CreatUser cu=new CreatUser();
                cu.setCreateUserName("");
                record.setCreatUser(cu);
            }else if(record.getUpdateUser()==null){
                UpdateUser u=new UpdateUser();
                u.setUpdateUserName("");
                record.setUpdateUser(u);
            }
        }
        return iPage;
    }

    @GetMapping("/add")
    public ModelAndView addPage(Integer id){
        ModelAndView mv=new ModelAndView("user/add");
        if(id!=null){
            User user = service.selectUserById(id);
            mv.addObject("user",user);
        }
        List<Role> roles = roleService.selectRole();
        mv.addObject("roles",roles);
        return mv;
    }

    //用户添加
    @RequestMapping("/save")
    public int save(User user){
        user.setCreateUserId(UserIdUntil.getUserId());
        user.setSalt(RandomSalt.getRandomSalt());
        Md5Hash md5Hash=new Md5Hash(user.getPassword(),user.getSalt(),2);
        user.setPassword(md5Hash.toString());
        user.setCreateTime(LocalDateTime.now());
        return service.addUser(user);
    }

    //用户更新
    @RequestMapping("/update")
    public int update(User user){
        System.out.println(UserIdUntil.getUserId());
        user.setUpdateUserId(UserIdUntil.getUserId());
        user.setUpdateTime(LocalDateTime.now());
        return service.update(user);
    }

    //用户删除
    @PostMapping("/delete")
    public int delete(Integer id){
        return service.delete(id);
    }

    //用户批量删除
    @RequestMapping("/deleteBetch")
    public int deleteBetch(@RequestParam(value = "ids[]") List<Integer> ids){
        return service.deleteBetch(ids);
    }

    //跳转到修改密码页面
    @GetMapping("/toPasswordPage")
    public ModelAndView openPwdPage(){
        ModelAndView mv=new ModelAndView("user/updatePwd");
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        mv.addObject("user",activeUser);
        return mv;
    }

    //修改密码
    @PostMapping("/updatePassword")
    public int updatePwd(PwdVo pwdVo){
        pwdVo.setSalt(RandomSalt.getRandomSalt());
        Md5Hash md5Hash=new Md5Hash(pwdVo.getNewPwd(),pwdVo.getSalt(),2);
        pwdVo.setNewPwd(md5Hash.toString());
        return service.updatePwd(pwdVo);
    }


    @PostMapping("/selectUserById")
    public String getPwd(Integer id,String pwd){
        String salt = service.getSalt(id);
        Md5Hash md5Hash=new Md5Hash(pwd,salt,2);
        return md5Hash.toString();
    }
}

